
var AV = require('av-weapp-min.js')
var app;

var userApi = {
  bind: function (name, cb) {
    if (!name) {
      cb({
        success: false,
        message: '姓名不能为空'
      });
      return;
    }

    // 更新用户
    var user = AV.User.current();

    user.set('nickName', name);
    user.save().then(function (user) {
      // 更新本地缓存
      app.globalData.userInfo.dbUser = user.toJSON()
      app.updateUserInfo(app.globalData.userInfo)

      cb({ success: true })
    }).catch(function () {
      cb({
        success: false,
        message: '绑定失败'
      })
    })



  },
  isBind: function () {
    if (!app.isLogin()) return false;
    if (!app.globalData.userInfo.dbUser) return false;
    var name = app.globalData.userInfo.dbUser.nickName
    console.log('name is ' + name)
    console.log(app.globalData.userInfo.dbUser)
    return name && name != ''
  }
}


module.exports = {
  user: {
    bind: userApi.bind,
    isBind: userApi.isBind
  },
  setApp: function (p) {
    app = p;
  }
}